﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace MCPro
{
    public class Hash
    {

        SHA512Managed hash;

        /// <summary>
        ///The default Constructor.
        /// </summary>
        public Hash()
        {
            hash = new SHA512Managed();
        }

        public string Cipher(string value)
        {
            string id = Guid.NewGuid().ToString();
            byte[] salt = UnicodeEncoding.UTF8.GetBytes(id);
            byte[] b = UnicodeEncoding.UTF8.GetBytes(value);
            int s = salt.Length;
            System.Array.Resize<Byte>(ref salt, salt.Length + b.Length + 1);

            b.CopyTo(salt, s);
            salt[salt.Length - 1] = (byte)b.Length;
            byte[] encrypted = hash.ComputeHash(salt);

            string strE = Convert.ToBase64String(encrypted);
            string strS = Convert.ToBase64String(salt);


            char[] q = strE.ToCharArray().Length.ToString().ToCharArray();
            byte[] tails = new byte[q.Length];

            for (int i = 0; i < q.Length; i++)
            {
                string c = q[i].ToString();
                tails[i] = Convert.ToByte(Convert.ToInt32(c));
            }

            string tail = Convert.ToBase64String(tails);

            string last = "";
            char[] ee = strE.ToCharArray();
            char[] ss = strS.ToCharArray();

            for (int i = 0; i < ee.Length; i++)
            {
                try
                {
                    string x = ee[i].ToString();
                    string y = ss[i].ToString();
                    last += (x + y);
                }
                catch (Exception)
                {
                    break;
                }
            }
            return last + tail;
        }

        public string Decipher(string value)
        {
            string q = value.Substring(value.Length - 4, 4);
            byte[] I = Convert.FromBase64String(q);
            string o = "";
            for (int i = 0; i < I.Length; i++)
            {
                o += I[i];
            }


            string a = "";
            string b = "";

            char[] x = value.Substring(0, value.Length - 4).ToCharArray();
            for (int i = 0; i < x.Length; i++)
            {
                a += x[i];
                b += x[i + 1];
                i++;
            }

            return "";
        }

        public void test()
        {
            byte[] a = new byte[] { 8, 8 };
            byte[] b = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            byte[] c = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0 };
            byte[] e = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0 };

            string f = Convert.ToBase64String(a);
            string g = Convert.ToBase64String(b);
            string h = Convert.ToBase64String(c);
            string i = Convert.ToBase64String(d);
            string j = Convert.ToBase64String(e);
        }

        /// <summary>
        ///The default Destructor.
        /// </summary>
        ~Hash()
        {
            hash.Dispose();
        }

    }
}